import { Context } from 'koa';
import { PermissionService } from '../../services/system/permissionService';
import { createError } from '../../middleware/errorHandler';
import { CreatePermissionDto, UpdatePermissionDto } from '../../dtos/system/PermissionDto';
import { generateResponse } from '../../utils';

export class PermissionController {
    private permissionService: PermissionService;

    constructor() {
        this.permissionService = new PermissionService();
    }

    // 获取所有权限
    getPermissions = async (ctx: Context): Promise<void> => {
        try {
            let data = ctx.request.body as any
            let dics = [] as any
            if (data?.page && data?.limit) {

                dics = await this.permissionService.getPermissionsWithPagination(data.page, data.limit);
                console.log(dics, 'dics');
            } else {
                dics = await this.permissionService.getAllPermissions();
            }
            generateResponse(ctx, dics);
        } catch (error: any) {
            throw createError('获取权限列表失败', 500, error);
        }
    };


    // 根据ID获取权限
    getPermissionById = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const role = await this.permissionService.getPermissionById(id);

            if (!role) {
                throw createError('权限不存在', 404);
            }

            generateResponse(ctx, role);
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('获取权限信息失败', 500, error);
        }
    };
    
    // 创建权限
    createPermission = async (ctx: Context): Promise<void> => {
        try {
            // 从验证中间件获取已验证的数据
            const roleData = ctx.state.validatedData as CreatePermissionDto;
            const role = await this.permissionService.createPermission(roleData);
            generateResponse(ctx, role, '权限创建成功');
        } catch (error: any) {

            throw createError('创建角色失败1', 400, error);
        }
    };

    // 更新权限
    updatePermission = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            // 从验证中间件获取已验证的数据
            const roleData = ctx.state.validatedData as UpdatePermissionDto;

            const role = await this.permissionService.updatePermission(id, roleData);

            if (!role) {
                throw createError('权限不存在', 404);
            }


            generateResponse(ctx, role, '权限更新成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('更新权限失败', 400, error);
        }
    };

    // 删除权限
    deletePermission = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const result = await this.permissionService.deletePermission(id);

            if (!result) {
                throw createError('权限不存在', 404);
            }


            generateResponse(ctx, null, '权限删除成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('删除权限失败', 500, error);
        }
    };
}